package com.zut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zut.common.CustomException;
import com.zut.entity.Category;
import com.zut.entity.Dish;
import com.zut.entity.Setmeal;
import com.zut.mapper.CategoryMapper;
import com.zut.mapper.DishMapper;
import com.zut.service.CategoryService;
import com.zut.service.DishService;
import com.zut.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;



    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(dishLambdaQueryWrapper);
        //查询当前分类是否关联了菜品，如果是，抛出一个业务异常
        if(count>0){
            //已经关联菜品，抛出一个业务异常
             throw new CustomException("当前分类关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(setmealLambdaQueryWrapper);
        //查询当前分类是否关联了套餐，如果是，抛出一个业务异常
        if(count1>0){
            //已经关联套餐，抛出一个业务异常
           throw new CustomException("当前分类关联了套餐，不能删除");

        }
        //删除分类
        super.removeById(id);
    }
}
